package com.example.lanouhn.bantangapp.activities.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FBeautywear;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FUseroiginal;
import com.example.lanouhn.bantangapp.activities.contants.Beautywear;
import com.example.lanouhn.bantangapp.activities.contants.Beautywear_Image;
import com.example.lanouhn.bantangapp.activities.contants.Useroriginal;
import com.example.lanouhn.bantangapp.activities.contants.Useroriginal_Image;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Beautywear;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Useroriginal;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/20.
 * 美妆数据绑定页面
 */
public class Fragment_Beautywear extends Fragment {
    private XRecyclerView recyclerview_beau;
    //viewPager的适配器
    private Adapter_FBeautywear adapter;

    private List<Beautywear> dataList;//文本

    //判断网络是否连接
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    adapter= new Adapter_FBeautywear(getActivity(),dataList);
                    recyclerview_beau.setAdapter(adapter);
                    break;
                case 0:
                    Toast.makeText(getActivity(),R.string.fail,Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    recyclerview_beau.refreshComplete();//消除下拉刷新
                    break;

            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_beautywear,container,false);
        recyclerview_beau = (XRecyclerView) view.findViewById(R.id.recyclerview_beau);
        recyclerview_beau.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview_beau.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(2);//标识
                    }
                }).start();
            }

            //上拉加载更多
            @Override
            public void onLoadMore() {

            }
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }
    //绑定数据
    private void initData() {

        dataList = new ArrayList<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtils.doGet(Beautywear_Image.BEAUTYWEAR_LIST_URL);

                dataList = JsonUtils_Beautywear.getBearList(result);
                if (dataList.size() > 0) {

                    Message msg = new Message();
                    handler.sendEmptyMessage(1);

                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();
    }
}
